#include "main.h"

//发送文件
void puts_file(int netFD, char* file_name){
    int fd = open(file_name, O_RDONLY);
    ERROR_CHECK(fd, -1, "open_putsFile.c");
    //获取文件大小
    struct stat file_st;
    fstat(fd, &file_st);

    //留文件存储大小
    off_t file_size;
    int ret = recv(netFD, &file_size, sizeof(off_t), MSG_WAITALL);
    ERROR_CHECK(ret, -1, "recv_filesize_getsFile.c");

    off_t flag = 0;
    if(file_size == -1){
        flag = 1;
    }else if(file_size < file_st.st_size){
        flag = file_size - file_size%4096;
        file_st.st_size -= file_size;
    }else{
        send(netFD, &flag, sizeof(flag), MSG_NOSIGNAL);
        return ;
    }

    send(netFD, &flag, sizeof(flag), MSG_NOSIGNAL);

    //发送文件大小
    ret = send(netFD, &(file_st.st_size), sizeof(file_st.st_size), MSG_NOSIGNAL);
    ERROR_CHECK(ret, -1, "send_putsFile.c");
    //发送文件内容
    ret = sendfile(netFD, fd, 0, file_st.st_size);
    ERROR_CHECK(ret, -1, "sendfile_putsFile.c");

    close(fd);

}
